package com.cise.sort.linear;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

import com.cise.utils.Util;

/**
 * ϵ����ֵ����
 * @author Venyo
 *
 */
public class CoefficientEvaluator {
	/** ��Ⱥ���� */
	private final static int POPULATION_NUM = 10;
	/** ��Ⱥ���� */
	private List<Chromosome> chromosomes = new ArrayList<Chromosome>();
	/** �������� */
	private final static int GENE_NUM = Util.SELECTED_ALGORITHMS.size();
	/** �������� */
	private final static int ITERATION_NUM = 20;
	/** ����ͼƬ�� */
	private List<String> similarPics = new ArrayList<String>();
	private String inputPic;
	
	public CoefficientEvaluator(String path) {
		this.inputPic = path;
	}
	
	public static void main(String[] args) {
		CoefficientEvaluator coefficientEvaluator = new CoefficientEvaluator("H:\\Nginx\\nginx-1.7.6\\nginx-1.7.6\\html\\pictures\\ͼƬ����\\���\\3.jpg");
		coefficientEvaluator.initSimilarPic("H:\\Nginx\\nginx-1.7.6\\nginx-1.7.6\\html\\pictures\\ͼƬ����\\���");
		coefficientEvaluator.start();
	}
	
	public void addSimilarPic(String path){
		this.similarPics.add(path);
	}
	
	public void initSimilarPic(String path){
		File file = new File(path);
		String[] paths = file.list();
		for(int i = 0; i < paths.length; i++){
			this.similarPics.add(path + File.separatorChar + paths[i]);
		}
	}
	
	public void start(){
		// ��ʼ����Ⱥ
		for(int i = 0; i < POPULATION_NUM; i++){
			Chromosome chromosome = new Chromosome(GENE_NUM);
			chromosome.randomCreateGenes();
			chromosome.calculateFitness(this.inputPic, this.similarPics);
			chromosomes.add(chromosome);
		}
		Scanner scanner = new Scanner(System.in);
		boolean b = true;
		while(b){
			for(int i = 0; i < ITERATION_NUM; i++){
				// ����
				sortChromosomes();
				// ��̭
				chromosomes = chromosomes.subList(0, POPULATION_NUM);
				printOut(i);
				// �����Ӵ�
				for(int j = 0; j < chromosomes.size()/2; j++){
					// ��������
					Chromosome chromosome = chromosomes.get(j*2).recombinant(chromosomes.get(j*2+1));
					// ����ͻ��
					chromosome.geneMutation();
					chromosome.calculateFitness(this.inputPic, this.similarPics);
					chromosomes.add(chromosome);
				}
			}
			sortChromosomes();
			printOut(ITERATION_NUM);
			
			System.out.print("continue?");
			b = scanner.nextBoolean();
		}
	}
	
	public void sortChromosomes(){
		Collections.sort(chromosomes, new Comparator<Chromosome>(){
			@Override
			public int compare(Chromosome arg0, Chromosome arg1) {
				double diff = arg0.getFitness() - arg1.getFitness();
				if(diff > 0){
					return -1;
				}
				if(diff == 0){
					return 0;
				}
				return 1;
			}
		});
	}
	
	public void printOut(int index){
		System.out.println("============��"+index+"��============");
		for(int i = 0; i < chromosomes.size(); i++){
			System.out.println(chromosomes.get(i).toString());
		}
		System.out.println();
	}
}
